package com.gdupt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.gdupt.entity.CustomerLoss;
import com.gdupt.entity.DrawPlan;
import com.gdupt.entity.SalesOpportunity;
import com.gdupt.entity.SystemUser;
import com.gdupt.service.ICustomerInfoService;
import com.gdupt.service.ICustomerLossService;
import com.gdupt.service.ISalesOpportunityService;
import com.gdupt.service.ISystemUserService;
import com.gdupt.until.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 销售机会 销售机会 前端控制器
 * </p>
 *
 * @author gdupt
 * @since 2018-11-21
 */
@RestController
@RequestMapping("/sales-opportunity")
public class SalesOpportunityController {
    @Autowired
    private ISalesOpportunityService sale;
    @Autowired
    private ISystemUserService systemUserService;
    @Autowired
    private ICustomerInfoService customerInfoService;

    @RequestMapping("/search")
    public ResponseData lossPage(@RequestParam(value = "sname",required = false) String sname,
                                 @RequestParam(value = "contact",required = false) String contact,
                                 @RequestParam(value = "people",required = false) String people,
                                   @RequestParam("page") Integer pageIndex,
                                   @RequestParam("limit") Integer pageSize) throws JsonProcessingException {
        Page page = new Page(pageIndex, pageSize);
        QueryWrapper<SalesOpportunity> qw = null;
        qw = new QueryWrapper<>();  //不能少了这句
        qw.isNull("found_name");
        if (Objects.nonNull(sname)||Objects.nonNull(contact)||Objects.nonNull(people)){

            qw.like("sname", sname).and(customerLossQueryWrapper -> customerLossQueryWrapper.like("contacts", people))
                    .and(customerLossQueryWrapper -> customerLossQueryWrapper.like("outline", contact));

        }

//        if (Objects.nonNull(search)) {
//            qw = new QueryWrapper<>();
//            qw.like("id", search).or().like("sname", search)
//                    .or().like("outline", search).or().like("contacts", search);
//        }
        IPage list = sale.page(page, qw);
        return ResponseData.okPage(list.getTotal(), list.getRecords());
    }

//模糊查询
    @RequestMapping("/find")
    public ResponseData find(@RequestParam(value = "sname",required = false) String sname,
                             @RequestParam(value = "contact",required = false) String contact,
                             @RequestParam(value = "people",required = false) String people){
        Map<String,Object> map = new HashMap<>();
        map.put("sname",sname);
        map.put("contact",contact);
        map.put("people",people);
        List<SalesOpportunity> list = sale.lossPage(map);
        System.out.println(list);
        return ResponseData.okData(list);

    }

    @RequestMapping("/list")
    public ResponseData loseList(Page page){
        List<SalesOpportunity> list=sale.lossList(page);
        return ResponseData.okPage(page.getTotal(),list);
    }

    @PostMapping("/delete/{id}")
    public  ResponseData deletePlan(@PathVariable("id") Integer id){
        boolean flg = sale.deletePlan(id);
        System.out.println(id);
        if(flg){
            return ResponseData.okMessage("删除成功！！");
        }
        return ResponseData.fail("删除失败！！");
    }

    @RequestMapping("/add")
    public ResponseData add(SalesOpportunity sa){
        boolean flg = false;
        if (sa.getSname()!=null&&sa.getSname()!=""
                &&sa.getOutline()!=null&&sa.getOutline()!=""
                &&sa.getOpportunitySource()!=null&&sa.getOpportunitySource()!=""
                )
        {
            flg= sale.add(sa);
        }



        if (flg){
            return ResponseData.okMessage("添加成功");
        }
        return ResponseData.fail("请把信息填写完整");
    }

    @PostMapping("/edit")
    public com.gdupt.entity.vo.ResponseData edit(SalesOpportunity sa){
        System.out.println(sa);

        boolean flg=false;
        if (sa.getSname()!=null&&sa.getSname()!=""
                &&sa.getSuccessProbability()!=null&&sa.getSuccessProbability()!=""
                &&sa.getOutline()!=null&&sa.getOutline()!=""
                &&sa.getOpportunitySource()!=null&&sa.getOpportunitySource()!="")
        {
            flg = sale.edit(sa);
        }


        if(flg){
            return com.gdupt.entity.vo.ResponseData.okMsg("操作成功！！");
        }
        return com.gdupt.entity.vo.ResponseData.failMsg("操作失败！！");
    }
    @RequestMapping("/user")
    public ResponseData findUser(){
        Map<String,Object> map=new HashMap<>();

        map.put("users",systemUserService.list());
        map.put("customers", customerInfoService.list());
        return  ResponseData.okData(map);
    }
    @RequestMapping("/listByName")
    public ResponseData loseListByName(Page page){
        List<SalesOpportunity> list=sale.lossListByName(page);
        return ResponseData.okPage(page.getTotal(),list);
    }


}
